<template>
  <view class="">
    <uni-nav-bar title="加注机检测标定" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
    <view class="tester">
      <view class="Flex">
        <text>加注机别名</text>
        <view class="" @click="openPhonePad()">
          <uni-icons type="compose" size="24" color="#2979ff"></uni-icons>
          <text style="margin-left: 20rpx">{{ fillingAll.code }}</text>
        </view>
      </view>
      <view class="Flex">
        <text>扣溶液存量</text>
        <switch :checked="checked" @change="stock" style="transform: scale(0.8)" color="#007AFF" />
      </view>
      <view class="Flex">
        <text>脉冲当量</text>
        <text>{{ fillingAll.pulse_equ }}</text>
      </view>
    </view>
    <view class="testerCenter">
      <view class="">
        <text style="width: 160rpx">出液量调整</text>
        <hpy-form-select
          :dataList="selectorObj"
          text="text"
          name="value"
          :value="'60'"
          @change="confirm"
          :title="output"
          v-if="select"
          :Text="output"
        />
        <text v-else>{{ output }}</text>
      </view>
      <view class="" style="color: #666; justify-content: flex-end">
        <text></text>
        <text></text>
      </view>
    </view>
    <view class="testerCenter">
      <view class="">
        <text style="width: 160rpx">加注量</text>
        <hpy-form-select
          :dataList="solutionList"
          :value="'1'"
          text="text"
          name="value"
          @change="consolu"
          :title="Filling"
          v-if="select"
          :Text="Filling"
        />
      </view>
      <view class="" style="color: #666; justify-content: flex-end">
        <text></text>
        <text>公斤</text>
      </view>
    </view>
    <view class="testerCenter">
      <view class="">
        <text style="width: 160rpx">出液量</text>
        <input type="text" placeholder="请填写实际出液量" v-model="capacity" />
      </view>
      <view class="" style="color: #666; justify-content: flex-end">
        <text></text>
        <text>公斤</text>
      </view>
    </view>
    <view class="submit" @click="submit('1')" v-if="status == ''">提交加注任务</view>
    <view class="submit" style="background: #fd9b00" v-else @click="submit('2')">保存结果</view>
    <view class="submit" style="background: rgba(27, 145, 255, 0.2); color: #1b91ff" @click="submit('3')">
      查看测试记录
    </view>
    <view class="submit" style="background: #fd9b00" @click="submit('4')">标定完成</view>
    <!-- 修改加注机别名 -->
    <uni-popup ref="show" type="center">
      <view class="popup">
        <text>加注机别名</text>
        <input type="text" placeholder="请输入加注机别名" v-model="fillName" />
        <view class="" @click="sures()">确定</view>
      </view>
    </uni-popup>
  </view>
</template>

<script setup lang="ts">
import { fillings } from '@/gql/filling';
import { getDriverID } from '@/stores/driverID';
import { navBack } from '@/utils/navigator';
import { showLoading, showToast, successToast } from '@/utils/prompt';
import { toPublish } from '@mqtt';
import { onMounted, ref } from 'vue';
// static
const driver_id = getDriverID();

const solutionList = [5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 500, 800, 1000, 1500, 2000]; // 加注量
// data
const fillingAll = ref({}); //加注机信息
const status = ref(''); //订单状态
const fillID = ref(''); //加注机ID
const capacity = ref(''); //实际出液量

const Filling = ref(0); //加注量
const output = ref(0); //出液量调整
const fillName = ref(''); //加注机别名
const selectorObj = [0];
// refs
const checked = ref(false); //扣溶液存量
const show = ref(null); //修改加注机别名弹框
const select = ref(false); //出液量调整弹框

const repeat = ref(true); //放重复点击
onMounted(() => {
  fillID.value = uni.getStorageSync('fillingID');
  selects();
  fillLists();
  FillingOrder();
});
/**
 * 计算出(-500~500)的出液量
 */
function selects() {
  const val = setInterval(() => {
    const fristnum = Number(selectorObj[0]);
    if (fristnum > -100) {
      const FristNumber = fristnum - 5;
      selectorObj.unshift(FristNumber + '');
    } else if (fristnum > -500) {
      const FristNumber = fristnum - 10;
      selectorObj.unshift(FristNumber + '');
    }
    const Lasternum = Number(selectorObj.slice(-1));
    if (Lasternum < 100) {
      const number = Lasternum + 5;
      selectorObj.push(number + '');
    } else if (Lasternum < 500) {
      const number = Lasternum + 10;
      selectorObj.push(number + '');
    } else {
      clearInterval(val);
      select.value = true;
    }
  }, 1);
}
/**
 * 获取加注机详情
 */
function fillLists() {
  showLoading('加载中');
  const filiD = uni.getStorageSync('fillingID');
  const payload = {
    query: fillings,
    variables: {
      id: filiD + '',
    },
  };
  toPublish('ql/driver/getFilling', payload, (obj: any) => {
    uni.hideLoading();
    const data = obj.data;
    fillingAll.value = data.fill_test;
    checked.value = data.fill_test.is_update === '1' ? true : false;
  });
}

//是否扣除溶液存量
function stock(e) {
  closePhonePad();
  showLoading();
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    fill_id: fillID.value + '',
    is_update: checked.value ? '0' : '1',
  };
  toPublish('order_test/editFill', payload, (obj: any) => {
    uni.hideLoading();
    const ref = obj;
    if (ref.code === 1) {
      repeat.value = true;
      showToast(ref.msg);
    } else {
      repeat.value = true;
      showToast(ref.msg);
    }
  });
}
//修改出液量
function confirm(e) {
  output.value = e.data;
  fillLists();
}
//加注量
function consolu(e) {
  Filling.value = e.data;
}
// 修改加注机别名
function sures() {
  if (!fillName.value) {
    showToast('请输入加注机别名');
    return;
  }
  closePhonePad();
  showLoading();
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    fill_id: fillID.value + '',
    code: fillName.value,
  };
  toPublish('order_test/editFill', payload, (obj: any) => {
    uni.hideLoading();
    const ref = obj;
    if (ref.code === 1) {
      repeat.value = true;
      showToast(ref.msg);
      fillLists();
    } else {
      repeat.value = true;
      showToast(ref.msg);
    }
  });
}

function openPhonePad() {
  show.value.open();
}
function closePhonePad() {
  show.value.close();
}
/**
 * @param {Object} ind 1 提交加注任务 2 保存结果 3 测试记录页面 4 标定可出厂
 */
function submit(ind) {
  switch (ind) {
    case '1':
      SubmitTask();
      break;
    case '2':
      SaveResults();
      break;
    case '3':
      uni.navigateTo({ url: '/pages/tester/record' });
      break;
    case '4':
      calibration();
      break;
  }
}
/**
 * 保存加注结果
 */
function SaveResults() {
  if (!capacity.value) {
    showToast('请输入实际出液量');
    return;
  }
  showLoading();
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    order_id: status.value,
    true_amount: capacity.value,
  };
  toPublish('order_test/editTestOrder', payload, (obj: any) => {
    uni.hideLoading();
    const ref = obj;
    capacity.value = '';
    Filling.value = 0;
    output.value = 0;
    if (ref.code === 1) {
      repeat.value = true;
      FillingOrder();
    } else {
      repeat.value = true;
      showToast(ref.msg);
    }
  });
}
/**
 * 提交加注任务
 */
function SubmitTask() {
  showLoading();
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    fill_id: fillID.value + '',
    nums: output.value,
  };
  toPublish('order_test/editChuye', payload, (obj: any) => {
    uni.hideLoading();
    const ref = obj;
    if (ref.code === 1) {
      repeat.value = true;
      placeOrder();
    } else {
      repeat.value = true;
      showToast(ref.msg);
    }
  });
}
//提交订单
function placeOrder() {
  if (!Filling.value) {
    showToast('请选择加注量');
    return;
  }
  showLoading();
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    fill_id: fillID.value + '',
    driver_id,
    amount: Filling.value,
  };
  toPublish('order_test/addTestOrder', payload, (obj: any) => {
    uni.hideLoading();
    const ref = obj;
    if (ref.code === 1) {
      repeat.value = true;
      FillingOrder();
    } else {
      repeat.value = true;
      showToast(ref.msg);
    }
  });
}
/**
 * 检测是否存在正在加注的订单
 */
function FillingOrder() {
  showLoading();
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    fill_id: fillID.value + '',
    driver_id,
  };
  toPublish('order_test/checkTestOrder', payload, (obj: any) => {
    const ref = obj;
    repeat.value = true;
    if (ref.code === 1) {
      status.value = ref.data.order_id ? ref.data.order_id : '';
      Filling.value = ref.data.order_id ? ref.data.amount : 0;
    }
  });
}
/**
 * 标定完成可出厂
 */
function calibration() {
  showLoading();
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    fill_id: fillID.value + '',
    driver_id,
  };
  toPublish('order_test/checkFillEnd', payload, (obj: any) => {
    const ref = obj;
    if (ref.code === 1) {
      successToast(ref.msg);
      setTimeout(() => {
		  repeat.value = true;
        uni.navigateTo({
          url: ref.data.page,
        });
      }, 1500);
    } else {
      repeat.value = true;
      showToast(ref.msg);
    }
  });
}
</script>

<style lang="less">
@import url('../tester/less/testing.less');
</style>
